package com.oocl.euc.core.attachment.controller;

import com.oocl.euc.core.attachment.model.Attachment;
import com.oocl.euc.core.attachment.service.AttachmentService;
import com.oocl.euc.framework.base.controller.RESTController;
import com.oocl.euc.framework.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("/rest/attachment")
public class AttachmentController extends RESTController<Attachment, Long> {
    @Autowired
    AttachmentService attachmentService;

    @Override
    protected BaseService<Attachment, Long> getBaseService() {
        return attachmentService;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseEntity uploadAttachment(@RequestParam("file") MultipartFile requestAttachment) throws IOException, NoSuchAlgorithmException {
        Attachment result = attachmentService.uploadAttachment(requestAttachment);
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/download", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity downloadAttachment(@RequestParam("fileId") Long fileId) {
        return attachmentService.downloadAttachment(fileId);
    }

}
